<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<table id="operator_table" class="table table-bordered">
</table>
<script type="text/javascript">
    $(function(){
        var table_cl = $('#operator_table').datagrid({
            height: '100%',
            width:'100%',
            fullGrid:true,
            columnMenu: false,
            contextMenuH:false,
            fieldSortable:false,
            gridTitle : '管理员管理',
            showToolbar: true,
            toolbarItem: 'refresh,|,del',
            toolbarCustom:function(){
                return '<button type="button" class="btn btn-blue" onclick="saveOperator();" data-icon="plus"><i class="fa fa-plus"></i> 添加</button>';
            },
            filterThead:false,
            inlineEditMult: false,
            addLocation: 'last',
            saveAll:false,
            delPK: 'id',
            dataUrl: '/admin/operator/operators',
            editUrl: '/admin/operator/save?id={id}',
            delUrl:'/admin/operator/delete',
            delConfirm:true,
            delCallback:function (response){
                if (response.haveError) {
                    BJUI.alertmsg('error', response.errMsg, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                } else {
                    BJUI.alertmsg('ok', response.data, {
                        okCall: function () {
                            BJUI.navtab('refresh')
                        }
                    });
                }
            },
            columns: [
                {
                    name: 'userName',
                    label: '管理员名称',
                    align: 'center'
                },{
                    /*name: 'roleModel.name',*/
                    render:roleName,
                    label: '权限名称',
                    align: 'center'
                },{
                    name: 'remark',
                    label: '说明',
                    align: 'center'
                },{
                    render:operation,
                    label:'操作列'
                }
            ],
            local:"remote",
            paging: {pageSize:5, pageIndex:1},
            linenumberAll: true
        });
    });

    /**
     * 显示角色列
     */
    function roleName(value,data){
        if(data.roleModel===null||data.roleModel === ""||typeof (data.roleModel) === "undefined"){
            return null;
        }
        return data.roleModel.name;
    }

    function operation(value,data){
        var roleId = null;
        if(data.roleModel!==null&&data.roleModel !== ""&&typeof (data.roleModel) !== "undefined"){
            roleId = data.roleModel.id;
        }
        var html = '<button type="button" class="btn-green" onclick="authorize(\''+data.id+'\');">权限配置</button>'
                +'<button type="button" class="btn-red" onclick="updatePwd(\''+data.id+'\');">设置密码</button>'
                + '<button type="button" class="btn-red" data-toggle="del.datagrid.tr">删除</button>';
        return html
    }

    /**
     * 添加管理员
     */
    function saveOperator(id) {
        var url = "/admin/operator/savePage";
        if(id !== ""&&id!==null&&typeof (id) !== "undefined" ){
            url = url + "?id="+id;
        }

        BJUI.dialog({
            id: 'add_operator',
            url: url,
            title: '添加',
            resizable: true,
            width: 850,
            height: 400,
            fresh:true
        });
    }
    
    /**
     * 授权
     * @param id
     */
    function authorize(id) {
        BJUI.dialog({
            id: 'authorize',
            url:"/admin/operator/addRolePage?id="+id,
            title: '权限设置',
            resizable: true,
            width: 850,
            height: 400,
            fresh:true
        });
    }

    /**
     * 修改门面
     */
    function updatePwd(id) {
        BJUI.dialog({
            id: 'changePwdPage',
            url:"/admin/operator/changePwdPage/"+id,
            title: '设置密码',
            resizable: true,
            width: 500,
            height: 300,
            fresh:true
        });
    }
    
</script>